import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";

/**
 * service 部门架构维护
 * @date: 2018-6-19

s
 */
import request from 'utils/request';
import { filterNullValueObject } from 'utils/utils';
import { CPAAS_PLATFORM } from 'utils/config'; // const CPAAS_PLATFORM = `/cpfm-10902`;

/**
 * 请求API前缀
 * @type {string}
 */

var prefix = "".concat(CPAAS_PLATFORM, "/v1");
/**
 * 获取公司信息
 * @async
 * @function gainCodeAndName
 * @param {object} params - 请求参数
 * @param {!number} params.tenantId - 租户Id
 * @param {!number} params.unitId - 组织Id
 * @returns {object} fetch Promise
 */

export function gainCodeAndName(_x) {
  return _gainCodeAndName.apply(this, arguments);
}
/**
 * 获取所有部门信息
 * @async
 * @function searchAll
 * @param {object} params - 请求参数
 * @param {!string} params.tenantId - 租户Id
 * @param {!number} param.companyUnitId - 组织Id
 * @returns {object} fetch Promise
 */

function _gainCodeAndName() {
  _gainCodeAndName = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(params) {
    return _regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            return _context.abrupt("return", request("".concat(prefix, "/").concat(params.tenantId, "/units/").concat(params.unitId), {
              method: 'GET' // query: params,

            }));

          case 1:
          case "end":
            return _context.stop();
        }
      }
    }, _callee);
  }));
  return _gainCodeAndName.apply(this, arguments);
}

export function searchAll(_x2) {
  return _searchAll.apply(this, arguments);
}
/**
 * 根据查询条件获取部门信息
 * @async
 * @function search
 * @param {object} params - 请求参数
 * @param {!string} params.tenantId - 租户Id
 * @param {!number} param.companyUnitId - 组织Id
 * @param {?string} param.unitCode - 部门编码
 * @param {?string} param.unitName - 部门名称
 * @returns {object} fetch Promise
 */

function _searchAll() {
  _searchAll = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(params) {
    return _regeneratorRuntime.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            return _context2.abrupt("return", request("".concat(prefix, "/").concat(params.tenantId, "/units/department/tree"), {
              method: 'GET',
              query: filterNullValueObject(params)
            }));

          case 1:
          case "end":
            return _context2.stop();
        }
      }
    }, _callee2);
  }));
  return _searchAll.apply(this, arguments);
}

export function search(_x3) {
  return _search.apply(this, arguments);
}
/**
 * 添加部门信息
 * @async
 * @function saveAdd
 * @param {Object} params - 请求参数
 * @param {!string} params.tenantId - 租户Id
 * @param {!object[]} params.data - 新增部门对象列表
 * @param {!string} params.data[].unitCode - 部门编码
 * @param {!string} params.data[].unitName - 部门名称
 * @param {!number} params.data[].orderSeq - 排序号
 * @param {!string} [params.data[].unitTypeCode = D] - 部门类型编码
 * @param {?number} [params.data[].supervisorFlag = 0] - 主管组织标记
 * @param {?number} [params.data[].enabledFlag = 1 ] - 启用标记
 * @param {?string} params.data[].companyUnitId - 所属组织Id
 * @param {?number} params.data[].parentUnitId - 上级部门Id
 * @param {?string} params.data[].parentUnitName - 上级部门名称
 * @returns {object} fetch Promise
 */

function _search() {
  _search = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(params) {
    return _regeneratorRuntime.wrap(function _callee3$(_context3) {
      while (1) {
        switch (_context3.prev = _context3.next) {
          case 0:
            return _context3.abrupt("return", request("".concat(prefix, "/").concat(params.tenantId, "/units/department"), {
              method: 'GET',
              query: filterNullValueObject(params)
            }));

          case 1:
          case "end":
            return _context3.stop();
        }
      }
    }, _callee3);
  }));
  return _search.apply(this, arguments);
}

export function saveAdd(_x4) {
  return _saveAdd.apply(this, arguments);
}
/**
 * 更新部门信息
 * @async
 * @function saveEdit
 * @param {Object} params - 请求参数
 * @param {!string} params.tenantId - 租户Id
 * @param {!object} params.values - 部门信息对象
 * @param {!number} params.values.unitId - 部门Id
 * @param {!string} params.values.unitCode - 部门编码
 * @param {!string} params.values.unitName - 部门名称
 * @param {!number} params.values.orderSeq - 排序号
 * @param {!string} params.values.unitTypeCode - 部门类型编码
 * @param {?number} params.values.parentUnitId - 上级部门Id
 * @param {?number} params.values.enabledFlag - 启用标记
 * @param {?string} params.values.companyUnitId - 所属组织Id
 * @returns {object} fetch Promise
 */

function _saveAdd() {
  _saveAdd = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4(params) {
    return _regeneratorRuntime.wrap(function _callee4$(_context4) {
      while (1) {
        switch (_context4.prev = _context4.next) {
          case 0:
            return _context4.abrupt("return", request("".concat(prefix, "/").concat(params.tenantId, "/units"), {
              method: 'POST',
              body: _toConsumableArray(params.data)
            }));

          case 1:
          case "end":
            return _context4.stop();
        }
      }
    }, _callee4);
  }));
  return _saveAdd.apply(this, arguments);
}

export function saveEdit(_x5) {
  return _saveEdit.apply(this, arguments);
}
/**
 * 禁用部门信息
 * @async
 * @function forbindLine
 * @param {Object} params - 请求参数
 * @param {!number} params.tenantId - 租户Id
 * @param {!number} params.unitId - 部门Id
 * @param {!string} params._token - token
 * @param {!number} params.objectVersionNumber - 版本号
 * @returns {object} fetch Promise
 */

function _saveEdit() {
  _saveEdit = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee5(params) {
    return _regeneratorRuntime.wrap(function _callee5$(_context5) {
      while (1) {
        switch (_context5.prev = _context5.next) {
          case 0:
            return _context5.abrupt("return", request("".concat(prefix, "/").concat(params.tenantId, "/units"), {
              method: 'PUT',
              body: _objectSpread({}, params.values)
            }));

          case 1:
          case "end":
            return _context5.stop();
        }
      }
    }, _callee5);
  }));
  return _saveEdit.apply(this, arguments);
}

export function forbindLine(_x6) {
  return _forbindLine.apply(this, arguments);
}
/**
 * 启用部门信息
 * @async
 * @function enabledLine
 * @param {!number} params.tenantId - 租户Id
 * @param {!number} params.unitId - 部门Id
 * @param {!string} params._token - token
 * @param {!number} params.objectVersionNumber - 版本号
 * @returns {object} fetch Promise
 */

function _forbindLine() {
  _forbindLine = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee6(params) {
    return _regeneratorRuntime.wrap(function _callee6$(_context6) {
      while (1) {
        switch (_context6.prev = _context6.next) {
          case 0:
            return _context6.abrupt("return", request("".concat(prefix, "/").concat(params.tenantId, "/units/disable"), {
              method: 'POST',
              body: params
            }));

          case 1:
          case "end":
            return _context6.stop();
        }
      }
    }, _callee6);
  }));
  return _forbindLine.apply(this, arguments);
}

export function enabledLine(_x7) {
  return _enabledLine.apply(this, arguments);
}
/**
 * 处理接口获取的数据，提取每个节点的层次路径
 * @param {array} collections - 页面展示数据
 * @param {array} levelPath - 特定组织的层级路径
 * @returns {object} 节点树和层次路径组成的对象
 */

function _enabledLine() {
  _enabledLine = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee7(params) {
    return _regeneratorRuntime.wrap(function _callee7$(_context7) {
      while (1) {
        switch (_context7.prev = _context7.next) {
          case 0:
            return _context7.abrupt("return", request("".concat(prefix, "/").concat(params.tenantId, "/units/enable"), {
              method: 'POST',
              body: params
            }));

          case 1:
          case "end":
            return _context7.stop();
        }
      }
    }, _callee7);
  }));
  return _enabledLine.apply(this, arguments);
}

export function renderTreeData() {
  var collections = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  var levelPath = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  // debugger;
  var pathMap = levelPath;
  var renderTree = collections.map(function (item) {
    var temp = item;
    pathMap[temp.unitId] = [].concat(_toConsumableArray(pathMap[temp.parentUnitId] || []), [temp.unitId]);

    if (temp.children) {
      temp.children = _toConsumableArray(renderTreeData(temp.children || [], pathMap).renderTree);
    }

    return temp;
  });
  return {
    renderTree: renderTree,
    pathMap: pathMap
  };
}